package nio.book.buffer;

import java.nio.ByteBuffer;
import java.nio.CharBuffer;

/**
 * byteBuffer转化为其他类型的buffer
 * 其他类型buffer的capacity = byteBuffer.capacity/其他buff类型所占字节数
 */
public class BufferCharView {
    public static void main(String[] args) {
        ByteBuffer byteBuffer = ByteBuffer.allocate(7);
        CharBuffer charBuffer = byteBuffer.asCharBuffer();
        byteBuffer.put(0, (byte) 0);
        byteBuffer.put(1, (byte) 'H');
        byteBuffer.put(2, (byte) 0);
        byteBuffer.put(3, (byte) 'i');
        byteBuffer.put(4, (byte) 0);
        byteBuffer.put(5, (byte) '!');
        byteBuffer.put(6, (byte) 0);
        System.out.println(byteBuffer.toString() + "--pos:" + byteBuffer.position() +
                "--limit:" + byteBuffer.limit() + "--cap:" + byteBuffer.capacity());
        System.out.println(charBuffer.toString() + "--pos:" + charBuffer.position() +
                "--limit:" + charBuffer.limit() + "--cap:" + charBuffer.capacity());
    }
}
